package graduate.cslg.com.universityshopping.dormitory;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CheckInActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText stuPhone;
    private EditText stuWantBed;
    private EditText stuReason;
    private Button submitButton;
    private String getUserAccount;
    private LoginResponse mLoginResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_in);
        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "Z09315222");

        initViews();
    }

    private void initViews() {
        stuPhone = findViewById(R.id.stu_tel);
        stuWantBed = findViewById(R.id.stu_want_bed);
        stuReason = findViewById(R.id.stu_reason);
        submitButton = findViewById(R.id.submit_button);
        submitButton.setOnClickListener(this);
    }

    private void postCheckInInfo(String userTel, String stuBed, String stuReason) {
        RetrofitDevelop.getTestService()
                .addCheckInInfo(userTel, stuBed, stuReason, getUserAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            Toast.makeText(CheckInActivity.this, "信息提交成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(CheckInActivity.this, "提交失败！", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(CheckInActivity.this, "网络错误，请稍后再试！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_button:
                postCheckInInfo(stuPhone.getText().toString(), stuWantBed.getText().toString(), stuReason.getText().toString());
                break;
        }
    }
}
